/**
 * 1，消息缓存
 * @param {Object} id 用户或群聊id
 * @param {Object} head 用户或群聊头像
 * @param {Object} msgType 消息类型
 * @param {Object} nickname 用户或群聊名称
 * @param {Object} news 新的消息
 * @param {Object} time 时间
 * @param {Object} type 类型（2，好友消息；3，群聊；4，系统消息）
 * @param {Object} msgNum 新的消息数
 * @param {Object} msgId 消息主键id
 * @param {Object} senderId 发送者id
 */
export function msgListFun(id, head, msgType, nickname, news, time, type, msgNum, msgId, senderId) {
	//console.log(msgType)
	var newNews = news
	var imagesUrl = ''
	var voice = ''
	var address = ''
	var hbMsg = ''
	if (msgType == 2) {
		imagesUrl = newNews
		newNews = '图片'
	}
	if (msgType == 3) {
		voice = newNews
		newNews = '语音'
	}
	if (msgType == 4) {
		address = newNews
		newNews = '地址'
	}
	if (msgType == 5) {
		hbMsg = newNews
		newNews = '红包'
	}

	var msgList = {
		id,
		head,
		msgType,
		nickname,
		news: newNews,
		time,
		type,
		msgNum,
		msgId,
		senderId,
		imagesUrl: imagesUrl,
		voice,
		address,
		hbMsg
	}
	return msgList
}



export default {
	//1，将数据存储到本地
	setMsgList(msgList) {
		/* console.log(msgList)
		console.log('for循环') */
		//console.log(msgList)
		var msg;
		var chatMsg = uni.getStorageSync('chatMessage');
		if (chatMsg == null || chatMsg == '') {
			msg = []
		} else {
			msg = chatMsg
		}
		var newChatMsg = []
		newChatMsg.push(msgList)
		//遍历数组，判断是否有相同的 用户id
		for (var i = 0; i < msg.length; i++) {
			//console.log(msg[i])
			if (msg[i].id != msgList.id && msg[i].type == msgList.type) {
				newChatMsg.push(msg[i])
				continue
			}
			if (msg[i].type !== msgList.type && msg[i].id != msgList.id) {
				newChatMsg.push(msg[i])
				continue
			}
			if (msg[i].type == msgList.type && msg[i].id != msgList.id) {
				newChatMsg.push(msg[i])
				continue
			}
		}
		uni.setStorageSync('chatMessage', newChatMsg);
		return
	},

	//2，获取本地存储数据
	getMsgList() {
		var chatMsg = uni.getStorageSync('chatMessage');
		if (chatMsg == null || chatMsg == '' || chatMsg.length == 0) {
			return []
		} else {
			return chatMsg
		}
	},

	//3，删除指定聊天
	removeMsgList(id, type) {
		var chatMsg = uni.getStorageSync('chatMessage');
		//遍历数组，判断是否有相同的 用户id
		for (var i = 0; i < chatMsg.length; i++) {
			if (chatMsg[i].id == id && chatMsg[i].type == type) {
				chatMsg.splice(i, 1);
			}
		}

		uni.setStorageSync('chatMessage', chatMsg);
		return
	},

	//4，聊天置顶
	topMsgList(id, type) {
		//let  arr = [{name:'章三',age:1},{name:'li',age:2},{name:'wang',age:4},{name:'liu',age:9}]
		let chatMsg = uni.getStorageSync('chatMessage');
		if (chatMsg.length <= 1) {
			return
		}

		let obj = {};
		chatMsg.forEach((item, index) => {
			if (item.id === id && item.type === type) {
				obj = item;
				chatMsg.splice(index, 1)
				return;
			}

		})

		chatMsg.unshift(obj);
		uni.setStorageSync('chatMessage', chatMsg);
		//console.log(arr);
	},

	//5，修改聊天未读消息数
	updateUnreadNum(obj) {
		var msgList = uni.getStorageSync('chatMessage')
		if (msgList.length == 0) {
			obj.msgNum = 1
			//console.log(obj)
			var newMsgList = []
			newMsgList.push(obj)
			uni.setStorageSync('chatMessage', newMsgList);
			return
		}

		var newMsgObj = obj

		for (var i = 0; i < msgList.length; i++) {
			if (msgList[i].id == obj.id && msgList[i].type == obj.type) {
				var oldMsgNum = msgList[i].msgNum
				var newMsgNum = oldMsgNum + 1
				newMsgObj.msgNum = newMsgNum
				msgList.splice(i, 1)
				msgList.unshift(newMsgObj)
				uni.setStorageSync('chatMessage', msgList);
				return
			}
		}
		obj.msgNum = 1
		msgList.push(obj)
		uni.setStorageSync('chatMessage', msgList);
		return
	},

	//6，消息已读
	msgRead(userId, type) {
		var msgList = uni.getStorageSync('chatMessage')
		if (msgList.length == 0) {
			return
		}
		for (var i = 0; i < msgList.length; i++) {
			if (msgList[i].id == userId && msgList[i].type == type) {
				msgList[i].msgNum = 0
				uni.setStorageSync('chatMessage', msgList);
				return
			}
		}
	}

}
